package com.huike.web.controller.clues;

import com.alibaba.excel.EasyExcel;
import com.huike.business.service.TbBusinessService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.domain.vo.ImportDataVO;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.utils.ClueExcelListener;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author EthanRuins
 * @date 2023/7/12
 * @apiNote
 */
@RestController
@RequestMapping("/clues/clue")
@AllArgsConstructor
@Api(tags = "线索管理相关接口")
public class TbClueController extends BaseController {
    private final ITbClueService clueService;
    private final TbBusinessService businessService;

    /**
     * 新增线索管理
     * @param clue 线索数据
     * @return success error
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:insert')")
    @PostMapping
    @ApiOperation(value = "新增线索管理")
    @Log(title = "新增线索管理", businessType = BusinessType.INSERT)
    public AjaxResult<Object> insert(@RequestBody TbClue clue) {
        if (!clueService.checkPhoneUnique(clue.getPhone())) {
            return AjaxResult.error("手机号已存在");
        }
        return toAjax(clueService.insert(clue));
    }

    /**
     * 修改线索管理
     * @param clue 线索数据
     * @return success error
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:update')")
    @PutMapping
    @ApiOperation(value = "修改线索管理")
    @Log(title = "修改线索管理", businessType = BusinessType.UPDATE)
    public AjaxResult<Object> update(@RequestBody TbClue clue) {
        return toAjax(clueService.update(clue));
    }

    /**
     * 批量分配
     * @param assignmentVo 分配线索ID和分配人ID
     * @return success
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:assign')")
    @PutMapping("/assignment")
    @ApiOperation(value = "批量分配")
    @Log(title = "批量分配", businessType = BusinessType.UPDATE)
    public AjaxResult<String> assign(@RequestBody AssignmentVo assignmentVo) {
        return AjaxResult.success(clueService.assign(assignmentVo.getIds(), assignmentVo.getUserId()));
    }

    /**
     * 批量捞取
     * @param assignmentVo 捞取线索ID和捞取人ID
     * @return success
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:gain')")
    @PutMapping("/gain")
    @ApiOperation(value = "批量捞取")
    @Log(title = "批量捞取", businessType = BusinessType.UPDATE)
    public AjaxResult<String> gain(@RequestBody AssignmentVo assignmentVo) {
        return AjaxResult.success(clueService.gain(assignmentVo.getIds(), assignmentVo.getUserId()));
    }

    /**
     * 线索转商机
     * @param id 线索ID
     * @return success
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:changeBusiness')")
    @PutMapping("/changeBusiness/{id}")
    @ApiOperation(value = "线索转商机")
    @Log(title = "线索转商机", businessType = BusinessType.UPDATE)
    public AjaxResult<Object> changeBusiness(@PathVariable Long id) {
        return AjaxResult.success(businessService.changeBusiness(id));
    }

    /**
     * 伪线索
     * @param id          伪线索ID
     * @param falseClueVo 原因及标注
     * @return success
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:false')")
    @PutMapping("/false/{id}")
    @ApiOperation(value = "伪线索")
    @Log(title = "伪线索", businessType = BusinessType.UPDATE)
    public AjaxResult<Object> falseClue(@PathVariable Long id, @RequestBody FalseClueVo falseClueVo) {
        return AjaxResult.success(clueService.falseClue(id, falseClueVo.getReason(), falseClueVo.getRemark()));
    }

    /**
     * 上传线索
     * @param file 上传的EXCEL
     * @return 成功行数，失败行数
     */
    @PostMapping("/importData")
    @ApiOperation(value = "上传线索")
    @Log(title = "上传线索", businessType = BusinessType.IMPORT)
    public AjaxResult<ImportDataVO> importData(MultipartFile file) throws Exception {
        ClueExcelListener excelListener = new ClueExcelListener(clueService, new ImportDataVO());
        EasyExcel.read(file.getInputStream(), TbClueExcelVo.class, excelListener).sheet().doRead();
        return AjaxResult.success(excelListener.getResult());
    }

    /**
     * 根据ID查询线索数据
     * @param id 线索ID
     * @return 线索数据
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:query')")
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询线索")
    public AjaxResult<TbClue> getById(@PathVariable Long id) {
        return AjaxResult.success(clueService.getById(id));
    }

    /**
     * 根据指定条件查询线索管理列表
     * @param clue 指定条件
     * @return 线索列表
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询线索管理列表")
    public TableDataInfo<List<TbClue>> getClueList(TbClue clue) {
        startPage();
        List<TbClue> list = clueService.getClueList(clue);
        return getDataTable(list);
    }

    /**
     * 根据指定条件查询线索池列表
     * @param clue 线索池条件
     * @return 线索池列表
     */
    @PreAuthorize("@ss.hasPermi('clues:clue:pool')")
    @GetMapping("/pool")
    @ApiOperation(value = "查询线索池列表")
    public TableDataInfo<List<TbClue>> getCluePool(TbClue clue) {
        startPage();
        List<TbClue> cluePool = clueService.getCluePool(clue);
        return getDataTable(cluePool);
    }
}
